/**
* 8.8 lkl 
* 管理员页面专用 
**/

// 几个用于变化宽的全局变量
var $Hleft = $('.header-left');
var $Hright = $('.header-right');
var $side = $('.admin-side');
var $body = $('body');

// 切换页面侧边栏响应
function changeTab(i){
	var $lis = $('.admin-side > ul > li');
	$.each($lis, function(index, val) {
		if(index == i) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	});
}
// 用于自动改变大小
function Auto(){
	var width = sessionStorage.getItem("boxW");
	if (width == 70){
        MinTab();
	} else {
        MaxTab();
	}
}
// 侧边栏放大缩小的滑动效
$('.admin-header > div.header-right > a').click(function(event) {
	if ($Hleft.outerWidth() > 70) {
		MinTab();
        sessionStorage.setItem("boxW", 70);
	} else {
		MaxTab();
        sessionStorage.setItem("boxW", 230);
	}
});
function MinTab(){
	$Hleft.css({
		'width': '70px',
		'padding-left': '70px'
	});
	$Hright.css({
		'left': '70px',
		'width': 'calc(100% - 70px)'
	});
	$side.css('width', '70px');
	$body.css('margin', '70px 0 0 70px');
}
function MaxTab(){
	$Hleft.css({
		'width': '230px',
		'padding-left': '90px'
	});
	$Hright.css({
		'left': '230px',
		'width': 'calc(100% - 230px)'
	});
	$side.css('width', '230px');
	$body.css('margin', '70px 0 0 230px');
}

function createPage(pageNo, totalPages) {
	var front = 4, back = 5, start = 1, end = totalPages;
	if (totalPages > 10) {
		if (pageNo <= front + 1) {
			start = 1; end = 10;
		} else if (totalPages < pageNo + back) {
			var add = back - (totalPages - pageNo) + front;
			start = pageNo - add;
			end   = totalPages;
		} else {
			start = pageNo - front;
			end   = pageNo + back;
		}
	}
	var pages = [];
	for (var i = start; i <= end; i++) {
    pages.push(i);
	}
	return pages;
}

/**
 * 150k以上的图片压缩0.4倍
 * @param file 文件对象
 * @param fun  回调函数，函数内传入图片的blob数据对象
 */
function imgZip(file, fun) {
	var scale = 0.4;
  if (file.size / 1024 <= 150) {
  	scale = 0.85;
  }
	var src = URL.createObjectURL(file);
	var image = new Image();
	image.src = src;
	image.onload = function () {
		var cv  = document.createElement("canvas");
		var ctx = cv.getContext("2d");
		var nw = 680;
		var nh = (nw / image.width) * image.height;
		cv.width = nw; cv.height = nh;
		ctx.drawImage(image, 0, 0, nw, nh);
		cv.toBlob(fun, "image/jpeg", scale);
	};
}